/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * RemoteFileLocationWizardPanel.java
 * 
 * Created on Jan 18, 2009 at 1:57:37 PM
 */

package org.robcash.apps.messagepopup.view.wizard;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.robcash.apps.messagepopup.view.components.RemoteFileLocationEditor;
import org.robcash.apps.messagepopup.vo.MessageStoreFactory;
import org.robcash.apps.messagepopup.vo.impl.RemoteFileMessageStoreLocation;
import org.robcash.commons.swing.wizard.Wizard;
import org.robcash.commons.swing.wizard.impl.BaseWizardPanel;
import org.robcash.commons.swing.wizard.model.WizardModel;

/**
 * Wizard panel for creating/editing the remote message store location
 * @author Rob
 */
public class RemoteFileLocationWizardPanel
        extends BaseWizardPanel<RemoteFileLocationEditor>
        implements ChangeListener {

    private RemoteFileLocationEditor editor;

    public RemoteFileLocationWizardPanel(String objectId) {
        super(objectId);
        editor = new RemoteFileLocationEditor();
        editor.addChangeListener(this);
        setComponent(editor);
    }

    @Override
    public String getNextPanelId() {
        return WizardModel.NEXT;
    }

    @Override
    public boolean canGoForward() {
        try {
            return editor.getRemoteFileLocation() != null;
        }
        catch(MalformedURLException e) {
            return false;
        }
    }

    /**
     * Indicates that the wizard panel is about to be hidden. This
     * implementation saves the message store location.
     * @param wizard Wizard that is about to hide this panel
     * @param saveWork Indicates whether or not work should be saved
     */
    @Override
    public void aboutToHide(Wizard wizard, boolean saveWork) {
        if ( saveWork ) {
            try {
                URL remoteURL = editor.getRemoteFileLocation();
                RemoteFileMessageStoreLocation storeLocation = new RemoteFileMessageStoreLocation(remoteURL);
                MessageStoreFactory factory = MessageStoreFactory.newInstance();
                ((SubscriptionModel) wizard.getModel()).setMessageStore(factory.newMessageStore(storeLocation));
            } catch (MalformedURLException ex) {
                Logger.getLogger(RemoteFileLocationWizardPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        super.aboutToHide(wizard, saveWork);
    }

    /**
     * Receive notification that something has changed.
     * @param e Change event
     */
    public void stateChanged(ChangeEvent e) {
        if ( e.getSource().equals(editor) ) {
            fireChangeListenerStateChanged(new ChangeEvent(this));
        }
    }


}
